import { ClubConfig } from "../../hall_server/config/ClubConfig";
import { Config } from "../../src/config/Config";
import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class ClubTopDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;


    public static async insertTop(param: object) {
        let clubid: number = param["clubid"]
        let userid: number = param["userid"]
        let score: number = param["score"]
        let wintimes: number = param["wintimes"]
        let gametimes: number = param["gametimes"]
        let dateStr: string = DateUtil.formatDateStr3(DateUtil.now())
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select * from t_club_top where `clubid`=? and `userid`=? and `date`=?"
        let sqlParams = [clubid, userid, dateStr];
        let rows = await client.queryPromise(sql, sqlParams);
        // Logger.log("insertTop===", sql, sqlParams, rows)
        if (rows && rows.length) {
            let id: number = rows[0]["id"]
            let newScore: number = score + rows[0]["score"]
            let newWintimes: number = wintimes + rows[0]["wintimes"]
            let newGametimes: number = gametimes + rows[0]["gametimes"]
            sql = "update t_club_top set `score`=?, `wintimes`=?, `gametimes`=?  where `id`=? "
            sqlParams = [newScore, newWintimes, newGametimes, id]
            await client.queryPromise(sql, sqlParams);
        } else {
            sql = "insert into t_club_top(clubid,userid,gametimes,wintimes,score, date) values(?,?,?,?,?,?)"
            sqlParams = [clubid, userid, gametimes, wintimes, score, dateStr]
            await client.queryPromise(sql, sqlParams);
        }
    }

    public static async updateTop(clubid: number, userid: number, key: string, value: number,dateStr:string) {
        let sql: string = "UPDATE t_club_top SET `" + key + "` = ? WHERE clubid = ? and userid=? and `date`=?";
        let sqlParams = [value, clubid, userid, dateStr];
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }

    }

    public static async get_club_top(clubid: number, topType: number, dateStr: string, page_size: number, cur_page: number) {
        let orderStr: string = "gametimes"
        if (topType == ClubConfig.Top_Type.gametimes) {
            orderStr = "gametimes"
        }
        else if (topType == ClubConfig.Top_Type.score) {
            orderStr = "score"
        } else if (topType == ClubConfig.Top_Type.wintimes) {
            orderStr = "wintimes"
        }

        let startIndex: number = (cur_page - 1) * page_size;
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select * from v_club_top where clubid=? and date=?  order by " + orderStr + " desc limit ?,?"
        let sqlParams = [clubid, dateStr, startIndex, page_size];
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("get_club_top==", sql, sqlParams)
        if (rows && rows.length) {
            return rows
        }
        return []
    }

    public static async get_club_top_one(clubid: number, userid: number, dateStr: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select * from v_club_top where clubid=? and userid=? and date=? "
        let sqlParams = [clubid, userid, dateStr];
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("get_club_top_one==", sql, sqlParams)
        if (rows && rows.length) {
            return rows
        }
        return []
    }

    public static async get_club_top_totalnum(clubid: number, dateStr: string) {
        let sql: string = "select count(*) from t_club_top where clubid=? and date=?"
        let sqlParams: Array<any> = [clubid, dateStr];
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("get_club_top_totalnum==", clubid, rows)
        if (rows) {
            return rows[0]["count(*)"]
        }
        return 0
    }


    public static async clear_club_top(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_club_top where date < ?"
        let sqlParams = [DateUtil.formatDateStr(delete_time)];
        await client.queryPromise(sql, sqlParams);
    }

}